package net.awpspace.mp3player.screen;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.List;

import net.awpspace.mp3player.Application;
import net.awpspace.mp3player.entity.FavoriteList;
import net.awpspace.mp3player.entity.Playlist;

public class MyPlaylistsScreen extends List implements ItemStateListener,
		CommandListener {
	
	private static MyPlaylistsScreen self;

	private static Vector list_my_playlist;
	private Image img_list_playing;
	private Image img_my_list;

	private Command select;
	private Command addPlaylist;
	
	public static MyPlaylistsScreen getInstance(){
		if(self==null)
			self = new MyPlaylistsScreen();
		return self;
	}

	public MyPlaylistsScreen() {
		super("My playlists", List.IMPLICIT);
		init();
	}

	private void init() {
		list_my_playlist = new Vector();
		Playlist playlist1 = new Playlist("Playlist1");
		Playlist playlist2 = new Playlist("Playlist2");
		Playlist playlist3 = new Playlist("Playlist3");
		
		list_my_playlist.addElement(playlist1);
		list_my_playlist.addElement(playlist2);
		list_my_playlist.addElement(playlist3);
		
		
		
		try {
			img_my_list = Image.createImage("/my_list.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// init command
		add_item();
		
		select = new Command("Select", Command.ITEM, 0);
		this.setCommandListener(this);
		this.setSelectCommand(select);
		addPlaylist = new Command("Add playlist", Command.SCREEN, 1);
		this.addCommand(addPlaylist);
		this.setCommandListener(this);
	}
	
	public void add_item(){
		
		for(int i=0; i<list_my_playlist.size();i++){
			Playlist playlist = (Playlist) list_my_playlist.elementAt(i);
			this.append(playlist.getName(), img_list_playing);
		}
	}

	public void commandAction(Command cmd, Displayable d) {
		// TODO Auto-generated method stub
		if (cmd == select) {
			int index = this.getSelectedIndex();
			System.out.println("index: " + index);
			Playlist playlist = (Playlist) list_my_playlist.elementAt(index);
			ListMusic lm2 = new ListMusic(playlist.getName(), List.IMPLICIT);
			lm2.createListMusic(playlist.getList());
			Application.getDisplay().setCurrent(lm2);
		} else {
			if (cmd == addPlaylist) {
				System.out.println("add");
				Application.getDisplay().setCurrent(new AddPlaylistScreen());
			}
		}
	}

	public void itemStateChanged(Item selected) {
		// TODO Auto-generated method stub
	}
	
	public void add_playlist(Playlist pl){
		list_my_playlist.addElement(pl);
		this.append(pl.getName(), img_list_playing);
	}

}
